package com.pingan.controller;

import com.pingan.pojo.SysLog;
import com.pingan.service.LogService;
import com.pingan.util.ExcelUtil;
import com.pingan.util.PageUtil;
import com.pingan.util.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/log")
public class LogController {
    @Autowired
    private LogService logService;

    @GetMapping("/list")
    public RespResult getLogList(@RequestBody SysLog sysLog){
        return new RespResult(PageUtil.startPage()
                .doSelectPageInfo(()->logService.getLogList(sysLog)));
    }

    @DeleteMapping("/delete")
    public RespResult doDeleteLog(Integer... ids){
        return new RespResult(logService.doDeleteLog(ids));
    }

    @GetMapping("/log/{id}")
    public RespResult getLogDesc(Integer id){
        return new RespResult(logService.getLogDesc(id));
    }

    @GetMapping("/export-params")
    public RespResult getExportParams(@RequestBody SysLog sysLog){
        return new RespResult(logService.getExportParams(sysLog));
    }

    @PostMapping("/export/{sign}")
    public RespResult doExport(@PathVariable String sign){
        return new RespResult(logService.doExport(sign));
    }

    @GetMapping("/export123")
    public void exportExcelData(HttpServletRequest request, HttpServletResponse response){
        // 定义表的标题
        String title = "员工列表一览";
        //定义表的列名
        String[] rowsName = new String[] { "序号", "姓名", "性别", "特长", "学历", "入职时间", "简历", "照片", "部门" };
        //定义表的内容
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = new Object[9];
//        objs[0] = "测试555555";
        objs[1] = 11;
        objs[2] = "111";
        objs[3] = "测试";
        objs[4] = "测试";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(2018-12-24);
        objs[5] = date;
        objs[6] = "测试";
        objs[7] = "测试";
        objs[8] = "测试";
        dataList.add(objs);
        // 创建ExportExcel对象
        ExcelUtil excelUtil = new ExcelUtil();

        try{
            String fileName= new String("测试excel文档.xlsx".getBytes("UTF-8"),"iso-8859-1");    //生成word文件的文件名
            excelUtil.exportExcel(title,rowsName,dataList,fileName,response);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
